// mircoApp/activityClock/clockAdd/clockAdd.js
const app = getApp();
import Dialog from '../../../dist/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    query:{
      id:'',
      detail:'',
      image_list:[]
    },
    pluginId:'',
    goodsId:'',
    orderId:'',
    actId:'',
    reissueId:'',
    imgs:[],
    content:'',
    taskShow:false,
    upNumMaxOther:0,
    coThemeColor:'',
    iPnoneBottomBol:false,
    message:'',
    maxHeight:{ maxHeight: 140, minHeight: 140 },
    // 图片上传
    uploadUrl: "upload.uploadPic",
    fileList3: [],
    imgPath:app.globalData.imgPath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let yz_basic_info = wx.getStorageSync("yz_basic_info");
    if(yz_basic_info) {
      this.setData({
        coThemeColor:yz_basic_info.theme_color
      })
    }
    if(options.id) {
      this.setData({
        'query.id':options.id,
        pluginId:options.pluginId,
        goodsId:options.goodsId,
        orderId:options.orderId,
      })
    }
    if(options.actId) {
      this.setData({
        actId:options.actId,
        reissueId:options.reissueId
      })
    }
    this.getiPnoneBottom();
  },
  textChange(event) {
    let val = event.detail;
    this.setData({
      'query.detail':val
    });
  },
  closeTask() {
    this.setData({
      taskShow:false
    })
  },
   //选择图片
   onRead(event) { //图片上传功能
    if (this.data.upNumMaxOther == 10 || this.data.upNumMaxOther >= 10) {
      wx.showToast({
        icon: "none",
        title: "图片数量已达到上限",
        duration: 1500,
      });
      return false;
    }
    let that = this;
    let urlStr = app.getNetAddresss(this.data.uploadUrl);
    wx.chooseMedia({
      count:  10 - this.data.upNumMaxOther,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFiles;
        console.log(tempFilePaths[0]);
        let photourl = tempFilePaths[0].tempFilePath;
        wx.showLoading({
          title: '加载中',
        })
        wx.uploadFile({
          url: urlStr,
          filePath: photourl,
          name: 'file',
          success(resdata) {
            let res = JSON.parse(resdata.data);
            wx.hideLoading();
            if (res.result == 1) {
              that.data.query.image_list.push(res.data.img_url);
              that.setData({
                upNumMaxOther: that.data.upNumMaxOther + 1,
                'query.image_list':that.data.query.image_list
              });

            } else {
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          }
        });
      }
    });
  },
  close_verification() {
    this.setData({
      taskShow:false
    })
  },    
  removeImg_1(e) {
    let index = e.currentTarget.dataset.index;
    this.data.query.image_list.splice(index,1);
    this.setData({
      upNumMaxOther: this.data.query.image_list.length,
    });
  },
  clockForm() {
    if (app._isTextEmpty(this.data.query.detail)) {
      app.tips('请填写内容');
      return;
    }
    if(this.data.query.image_list.length == 0) {
      app.tips('请上传图片');
      return;
    }
    if(this.data.actId) {
      let params = {
        check_in_id:this.data.actId,
        detail:this.data.query.detail,
        image_list:this.data.query.image_list
      }
      let urlStr = app.getNetAddresss("plugin.check-in-rebate.frontend.participate-in.supplement");
      app._postNetWork({
        url: urlStr,
        data: params,
        success: (resdata) => {
          var res = resdata.data;
          let data = res.data;
          if (res.result == 1) {
            if(data.is_give) {
              this.setData({
                taskShow:true
              })
            } else {
              this.goBack();
            }
            app.tips(res.msg);
          } else {
            if(data.type =='no_supplement') {
              Dialog.confirm({
                message: '可购买指定商品卡',
              })
              .then(() => {
                // on confirm
                wx.navigateTo({
                  url: `/packageA/detail_v2/detail_v2?id=${this.data.reissueId}`,
                })
                
              })
              .catch(() => {
                // on cancel
              });
            } else {
              
            }
            
          }
          
          
        },
        fail: function (res) {
          console.log(res);
        },
      });
    } else {
      let urlStr = app.getNetAddresss("plugin.check-in-rebate.frontend.participate-in.check-in");
      app._postNetWork({
        url: urlStr,
        data:this.data.query,
        success: (resdata) => {
          var res = resdata.data;
          let data = res.data;
          if (res.result == 1) {
              if(data.is_give) {
                this.setData({
                  taskShow:true
                })
              } else {
                this.goBack();
              }
            
          }
          app.tips(res.msg)
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
    
  },
  getiPnoneBottom() {
    let that = this;
    try {
      let value = wx.getStorageSync('iPhoneBottom');
      if (value) {
        that.setData({
          iPnoneBottomBol: true
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  //跳转售后
  refundOn() {
    wx.navigateTo({
      url: `/packageD/member/myOrder/refund/refund?order_id=${this.data.orderId}&type=0&pluginid=${this.data.pluginId}&partId=${this.data.query.id}&goodsId=${this.data.goodsId}`,
    });
  },
  goBack() {
    let pages = getCurrentPages();
    if (pages.length <= 1) {
      wx.reLaunch({
        url: '/packageG/index/index',
      });
    } else {
      wx.navigateBack({ //返回
        delta: 1
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      taskShow:false,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})